package com.cls.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cls.business.entity.*;
import com.cls.business.mapper.EntrustTopicMapper;
import com.cls.business.mapper.TopicPlanMapper;
import com.cls.business.service.IDeclareTopicService;
import com.cls.business.service.IEntrustTopicService;
import com.cls.business.service.ITopicSubjectDetailService;
import com.cls.common.entity.QueryRequest;
import com.cls.common.enums.DictEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 *  Service实现
 *
 * @author slx
 * @date 2020-12-10 17:17:35
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EntrustTopicServiceImpl extends ServiceImpl<EntrustTopicMapper, EntrustTopic> implements IEntrustTopicService {

    private final IDeclareTopicService declareTopicService;

    private final ITopicSubjectDetailService topicSubjectDetailService;

    private final TopicPlanMapper topicPlanMapper;

    @Override
    public PageInfo<EntrustTopic> findEntrustTopics(QueryRequest request, EntrustTopic entrustTopic) {
        LambdaQueryWrapper<EntrustTopic> queryWrapper = new LambdaQueryWrapper<>();

        // TODO 设置查询条件
        // 选题编号
        if(StringUtils.isNotEmpty(entrustTopic.getTopicCode())){
            queryWrapper.like(EntrustTopic::getTopicCode,entrustTopic.getTopicCode());
        }
        if (StringUtils.isNotEmpty(entrustTopic.getTopicClassifyCode())) {
            // 添加不限
            queryWrapper.and(wrapper -> {
                queryWrapper.eq(EntrustTopic::getTopicClassifyCode, entrustTopic.getTopicClassifyCode()).or()
                        .eq(EntrustTopic::getTopicClassifyCode, DictEnum.UNLIMITED.getCode());
            });
        }

        // 选题名称
        if(StringUtils.isNotEmpty(entrustTopic.getTopicName())){
            queryWrapper.like(EntrustTopic::getTopicName,entrustTopic.getTopicName());
        }

        // 选题状态
        if(StringUtils.isNotEmpty(entrustTopic.getStatusTypeCode())){
            queryWrapper.like(EntrustTopic::getStatusTypeCode,entrustTopic.getStatusTypeCode());
        }

        // 期次
        if(StringUtils.isNotEmpty(entrustTopic.getStage())){
            queryWrapper.eq(EntrustTopic::getStage,entrustTopic.getStage());
        }

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<EntrustTopic> list= this.list(queryWrapper);
        for(EntrustTopic one:list){
            TopicPlan plan = topicPlanMapper.selectById(one.getPlanId());
            one.setPlanStatusCode(plan.getStatusCode());
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<EntrustTopic> findEntrustTopics(EntrustTopic entrustTopic) {
	    LambdaQueryWrapper<EntrustTopic> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        // 课题指南编号
        if(StringUtils.isNotEmpty(entrustTopic.getTopicName())){
            queryWrapper.like(EntrustTopic::getTopicCode,entrustTopic.getTopicCode());
        }

        // 期次
        if(StringUtils.isNotEmpty(entrustTopic.getStage())){
            queryWrapper.eq(EntrustTopic::getStage,entrustTopic.getStage());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createEntrustTopic(EntrustTopic entrustTopic) {
        this.save(entrustTopic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEntrustTopic(EntrustTopic entrustTopic) {
        this.saveOrUpdate(entrustTopic);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEntrustTopics(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean autoWriteSubjectType(List<EntrustTopic> topics, List<DeclareTopic> declareTopics, List<TopicSubjectDetail> details) {
        if (topics != null) {
            topics.forEach(item->{
                saveOrUpdate(item);
            });
        }

        if (declareTopics != null) {
            declareTopics.forEach(item->{
                declareTopicService.saveOrUpdate(item);
            });
        }

        if (details != null) {
            details.forEach(item->{
                topicSubjectDetailService.saveOrUpdate(item);
            });
        }

        return true;
    }

    @Override
    public List<EntrustTopic> findEntrustTopicsByPlanId(Long planId) {
        return baseMapper.selectByPlanId(planId);
    }

}
